<template>
  <div>
    <el-container>
      <el-header>
        <img class="logo" src="/images/logo.png" alt="管理员登录界面">
      </el-header>
      <el-main>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="用户名 " prop="username">
            <el-input placeholder="请输入用户名" v-model="ruleForm.username"  clearable></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input placeholder="请输入密码" v-model="ruleForm.password" show-password></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-main>
      <el-footer>基于SpringBoot的电影推荐系统 管理员登录界面</el-footer>
    </el-container>

  </div>
</template>

<script>
export default {
  name: "AdminLogin.vue",
  data() {
    return {
      ruleForm: {
        username: '',
        password:''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 4, max: 18, message: '长度在 4 到 18 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const _this=this
          debugger;
          this.$axios.post("/adminLogin",this.ruleForm).then(res =>{
            if(res.data.code===200){
              this.$message({
                showClose: true,
                message: '登录成功',
                type: 'success'
              });
              const jwt=res.headers['authorization']
              const userInfo=res.data.data
              _this.$store.commit("SET_TOKEN",jwt)
              _this.$store.commit("SET_USERINFO",userInfo)
              _this.$router.push("/films")
            }else{
              this.$message.error(res.data.message);
            }
          })
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
<style>
.el-header, .el-footer {
  background-color: #B3C0D1;
  color: #333;
  text-align: center;
  line-height: 60px;
}


.el-aside {
  background-color: #D3DCE6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  color: #333;
  text-align: center;
  line-height: 60px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
.logo {
  height: 90%;
  margin-top: 10px;
}
.demo-ruleForm{
  max-width: 500px;
  margin: 0 auto;
}
</style>